<?php

namespace app\partners\controller;

use app\common\controller\Adminbase;
use app\model\DistributionSetting;
use app\model\User as UserModel;

class Setting extends Adminbase
{
    public function form()
    {
        $uuid = 'partner';
        $info = DistributionSetting::where('uuid', $uuid)->find();
        if (empty($info)) {
            $info = [
                'uuid'  => $uuid,
                'name'  => '团长设置',
                'type'  => 0,
                'value' => json_encode([]),
            ];
            DistributionSetting::create($info);
        } else {
            $info['value'] = json_decode($info['value'], true);
        }

        $model = 'ShopCategory';

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['ratio'] > 100 || $data['ratio'] < 0) {
                return json(['code' => -1, 'msg' => '比例范围为0-100']);
            }
            $save = [
                'url'      => $data['url'],
                'shop_url' => $data['shop_url'],
                'ratio'    => $data['ratio'],
                'title'    => $data['title'],
            ];
            DistributionSetting::where('uuid', $uuid)->update([
                'value' => json_encode($save, JSON_UNESCAPED_UNICODE),
            ]);
            return json(['code' => 0, 'msg' => '保存成功']);
        }

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setUrl('form')
            ->setModel($model)
            ->setSuccessUrl('form')
            ->addFormItem('url', 'text', '新手教程', "<span style='color: red'>可在腾讯文档编辑教程文档，然后将文档地址填写到此处</span>")
            ->addFormItem('shop_url', 'text', '邀请店铺教程', "<span style='color: red'>可在腾讯文档编辑教程文档，然后将文档地址填写到此处</span>")
            ->addFormItem('ratio', 'gtnumber', '团长分成比例（%）', "<span style='color: red'>如果设置80%，订单总佣金为10元，则团长和推客总佣金为8元，剩余2元归推客系统所有</span>")
            ->addFormItem('title', 'text', '团长站点名称')
            ->setFormData($info['value'])
            ->returnForm();
        $this->assign('no_return', 1);
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }
}
